<?php

namespace app\user\model;

use think\Model;
use think\Session;
use app\common\traits\model\TraitModel;
use \app\common\traits\model\UserOperateCheck;

class CaravanInfos extends Model
{
    use TraitModel,UserOperateCheck;
    protected $autoWriteTimestamp = 'timestamp';
    protected $createTime = 'created_at';
    protected $updateTime = 'updated_at';
    protected $table = 'caravan';

    protected $type = [
            'piccaravan'=>'array',
            'caravancover'=>'array'
    ];
   	/**
     * [addCaravan 创建房车]
     * @param [type] $caravan_info_array [description]
     */
    public function camp()
    {
      return $this->belongsTo('Camps', 'services');
    }
   	/**
     * [addCaravan 创建房车]
     * @param [type] $caravan_info_array [description]
     */
    public function addCaravan($caravan_info_array)
    {
    	return $this->create($caravan_info_array);
    }
    /**
     * [findCaravan 查找房车]
     * @param  [type] $user_id [description]
     * @return [type]          [description]
     */
    public function findCaravan($user_id)
    {
        if ($this::get(['user_id'=>$user_id])) {
            return $this->where('user_id',$user_id)->find();
        }else{
            $this->data([
                'user_id'   =>$user_id,
                'wifi'      =>'2',
                'tv'        =>'2',
                'washer'    =>'2',
                'air_condition'=>'2',
                'the_shower'=>'2',
                'extinguisher'=>'2',
                'annunciator'=>'2',
                'monitored'=>'2'
                ]);
            $this->save();
            return $this->where('user_id',$user_id)->find();
        }
    }
    /**
     * [getCaravanInfo 获取房车信息]
     * @param  [type] $caravan_id [description]
     * @param  string $field      [description]
     * @return [type]             [description]
     */
    public function getCaravanInfo($caravan_id, $field = '')
    {
      $caravanInfo = self::get($caravan_id);
      if ($field) {
          if (isset($caravanInfo->$field)) {
              return $caravanInfo->$field; 
          }
          return ;
      }
      return $caravanInfo;
    }
    /**
     * 创建随机数
     * @param  integer $length [description]
     * @return [type]          [description]
     */
    public static function quickRandom($length = 16)
    {
        $pool = '0123456789';

        return substr(str_shuffle(str_repeat($pool, $length)), 0, $length);
    }
    //更新房车信息
    public function updateCaravan($user_id,$caravan_info_array)
    {
    	return $this->update($caravan_info_array,['user_id' =>$user_id]);
    }

    //房车类型---房车品牌---房车国别---准驾类型
    /**
     * [getGrade 类型转换]
     * @param  [type] $id [description]
     * @return [type]     [description]
     */
    public function getCates($id)
    {
        $grades = db('car_cates')->column('name', 'id');
        if (array_key_exists($id, $grades)) {
           return $grades[$id];
        }
        return '无';
    }
    public function getBrands($id)
    {
        $grades = db('car_brands')->column('name', 'id');
        if (array_key_exists($id, $grades)) {
           return $grades[$id];
        }
        return '无';
    }
    public function getCountries($id)
    {
        $grades = db('car_countries')->column('name', 'id');
        if (array_key_exists($id, $grades)) {
           return $grades[$id];
        }
        return '无';
    }
    public function getDrtypes($id)
    {
        $grades = db('driver_types')->column('name', 'id');
        if (array_key_exists($id, $grades)) {
           return $grades[$id];
        }
        return '无';
    }
    /**
     * [getZiduan1 description]
     * @return [type] [description]
     */
    public function getZiduan1()
    {
    $ziduan1=array(
    /////房车室内信息////////
    'wifi'=>['1'=>'有','2'=>'无'],
    'tv'=>['1'=>'有','2'=>'无'],
    'washer'=>['1'=>'有','2'=>'无'],
    'air_condition'=>['1'=>'有','2'=>'无'], 
    'the_shower'=>['1'=>'有','2'=>'无'],
     );
         return $ziduan1;
         return $this->all();
     }
    /**
      * [getZiduan2 description]
      * @return [type] [description]
      */
    public function getZiduan2()
    {
    $ziduan2=array(
    /////安全设施///////////
    'extinguisher'=>['1'=>'有','2'=>'无'],
    'annunciator'=>['1'=>'有','2'=>'无'], 
    'monitored'=>['1'=>'有','2'=>'无'],
     );
         return $ziduan2;
         //return $this->all();
    }
    /**
      * [getZiduan2 description]
      * @return [type] [description]
      */
    public function getZiduan3()
    {
        $ziduan3 = ['1'=>'手动挡','2'=>'自动挡','3'=>'手自一体'];
        return $ziduan3;
    }
    public function getZiduan3Name($value)
    {
        $status = ['1'=>'手动挡','2'=>'自动挡','3'=>'手自一体'];
        return $status[$value];
    }
    public function getRadioName($value)
    {
        if(empty($value)){
            return '无';
        }
        $status = ['1'=>'有','2'=>'无'];
        return $status[$value];
    }

  
}
